<template>
    <div class="columns py-6">
        <div class="column is-half is-offset-one-quarter">
            <el-card shadow="never">
                <div slot="header" class="has-text-centered has-text-weight-bold">
                    用户登录
                </div>
                <div>
                    <el-form v-loading="loading" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="账户" prop="name" >
                            <el-input type="text" v-model="ruleForm.name" value="ergou"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="pass">
                            <el-input v-model="ruleForm.pass" placeholder="请选择输入密码" type="password" value="123456"></el-input>
                        </el-form-item>
                        <el-form-item label="记住" prop="delivery">
                            <el-switch v-model="ruleForm.rememberMe"></el-switch>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-card>
        </div>

    </div>
</template>

<script>

    export default {
        name: "Login",
        data(){
            return{
                //用户注册中，防止用户重新注册，需等待后台回应后再进行下步操作
                loading: false,
                redirect: undefined,
                ruleForm: {
                    name: '',
                    pass: '',
                    rememberMe: true
                } ,
                rules:{
                    name: [
                        { required: true, message: '请输入账户', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    pass:  [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
                    ]
                }
            }
        },
        methods: {

            submitForm(formName) {
                //校验信息
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading = true
                        //向vue的store发送请求
                        this.$store
                        //指定modules：user，login方法
                        .dispatch("user/login", this.ruleForm)
                        .then(response =>{
                            console.log(response)
                            this.$message({
                                message: response.message,
                                type: 'success',
                                duration: 3000
                            })

                            //账号登录成功后，启动定时器
                            setTimeout(() =>{
                                this.loading = false
                                this.$router.push({
                                    path: this.redirect || '/'
                                })
                            },0.1*1000)
                        })
                        //最后解开loading
                        .catch(() =>{
                            this.loading = false
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>